const api = require('../../../api/mop_api');
import { toast, log, err } from '../../../utils/wxapi';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    size: 20,
    list: [],
    pageTotal: 0,
    nav: [
      { name: '全部', status: '' },
      { name: '待审核', status: 'AWAITING_MODERATION' },
      { name: '审核通过', status: 'PASS_AUDIT' },
      { name: '售后完成', status: 'RETURN_SUCCESS' },
      { name: '售后关闭', status: 'CLOSE' },
    ],
    navnum: 0,
    cha: '',
  },
  onShow() {
    this.pay_return_list();
  },
  navtap(e) {
    try {
      const i = e.currentTarget.dataset.index;
      this.setData({ navnum: i, list: [], page: 1 });
      if (i === 0) {
        this.pay_return_list();
      } else {
        const sta = this.data.nav[i].status;
        this.status_list(sta);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  async status_list(status) {
    try {
      const { page, size, list } = this.data;
      const data = { status };
      const res = await api.payrefundList({ page, size, data });
      if (res.respCode === 200) {
        const newlist = JSON.parse(res.source);
        const pageTotal = res.pageTotal;
        const updatedOdlist = list.length === 0 ? newlist : list.concat(newlist);
        this.setData({
          list: updatedOdlist,
          pageTotal,
        });
      }
    } catch (error) {
      err(error);
    }
  },
  async pay_return_list() {
    try {
      const { page, size, list } = this.data;
      const res = await api.payrefundList({ page, size });
      if (res.respCode === 200) {
        const newlist = JSON.parse(res.source);
        console.log(list);
        const pageTotal = res.pageTotal;
        log(newlist);
        const updatedOdlist = list.length === 0 ? newlist : list.concat(newlist);
        this.setData({
          list: updatedOdlist,
          pageTotal,
        });
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  pass(e) {
    try {
      const { id } = e.currentTarget.dataset;
      const status = 'PASS_AUDIT';
      wx.showModal({
        title: '请输入审核通过原因',
        editable: true,
        complete: async res => {
          if (res.confirm) {
            const data = {
              id,
              status,
              closeReason: res.content,
            };
            const { respCode, msg } = await api.payrefund(data);
            if (respCode === 200) {
              this.setData({ list: [], page: 1 });
              // this.pay_return_list();
              toast('审核通过');
            } else {
              toast(msg);
            }
          }
        },
      });
    } catch (error) {
      err(error);
      throw error;
    }
  },
  no_pass(e) {
    const { id } = e.currentTarget.dataset;
    const status = 'CLOSE';
    try {
      wx.showModal({
        title: '请输入审核不通过原因',
        editable: true,
        complete: async res => {
          if (res.confirm) {
            const data = {
              id,
              status,
              closeReason: res.content,
            };
            const res = await api.payrefund(data);
            if (res.respCode === 200) {
              this.setData({ list: [], page: 1 });
              this.pay_return_list();
              toast('审核不通过');
            } else {
              toast(res.msg);
            }
          }
        },
      });
    } catch (error) {
      err(error);
      throw error;
    }
  },
  async phone() {
    const { cha } = this.data;
    try {
      const res = api.payrefundList({ memberId: cha });
      if (res.respCode === 200) {
        const list = JSON.parse(res.source);
        log(list);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let { list, pageTotal, page } = this.data;
    if (list.length < pageTotal) {
      this.setData({ page: page + 1 });
      this.pay_return_list();
    } else {
      toast('已加载全部数据');
    }
  },
});
